package com.greentravel.service.impl;

import com.greentravel.dao.UserDAO;
import com.greentravel.dao.impl.UserDAOImpl;
import com.greentravel.model.User;
import com.greentravel.service.UserService;

import java.util.List;

/**
 * 用户服务实现类
 * 实现与用户相关的具体业务逻辑
 */
public class UserServiceImpl implements UserService {

    private UserDAO userDAO;

    public UserServiceImpl() {
        userDAO = new UserDAOImpl();
    }

    @Override
    public boolean register(User user) {
        // 先检查用户名是否已存在
        if (isUsernameExist(user.getUsername())) {
            return false;
        }
        return userDAO.addUser(user);
    }

    @Override
    public User login(String username, String password) {
        return userDAO.login(username, password);
    }

    @Override
    public boolean updateUserInfo(User user) {
        return userDAO.updateUser(user);
    }

    @Override
    public User getUserInfo(int userId) {
        return userDAO.getUserById(userId);
    }

    @Override
    public int getUserPoints(int userId) {
        User user = userDAO.getUserById(userId);
        if (user != null) {
            return user.getPoints();
        }
        return 0;
    }

    @Override
    public boolean addUserPoints(int userId, int points) {
        return userDAO.updateUserPoints(userId, points);
    }

    @Override
    public List<User> getUserRanking() {
        return userDAO.getUsersOrderByPoints();
    }

    @Override
    public boolean isUsernameExist(String username) {
        return userDAO.isUsernameExist(username);
    }
}